﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;
using _3DHouse.CustomControl;
using System.IO;
using System.Windows.Markup;
using System.Windows.Media.Media3D;
using _3DHouse.Lib;

namespace _3DHouse
{
    /// <summary>
    /// 导入工具.xaml 的交互逻辑
    /// </summary>
    public partial class ImportTools : Window
    {
        public ImportTools()
        {
            InitializeComponent();
        }
        Cube cube;
        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog open = new OpenFileDialog();
                open.Filter = "3DMax|*.obj";
                if (open.ShowDialog().Value)
                { 
                    World.Children.Remove(cube);
                    txtFile.Text = open.FileName;
                
                WavefrontObjLoader wfl = new WavefrontObjLoader();
                 cube = wfl.LoadObjFile(txtFile.Text);

                 World.Children.Add(cube);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {           
            ImportOBJ();
        }
        /// <summary>
        /// 导入obj格式
        /// </summary>
        private void ImportOBJ()
        {
            if (txtName.Text.Trim().Equals(""))
            {
                MessageBox.Show("请输入名称");
                return;
            }
            if (txtFile.Text.Trim().Equals(""))
            {
                MessageBox.Show("请选择文件");
                return;
            }
            //保存路径
            string path = Directory.GetCurrentDirectory() + "\\tools";
            WavefrontObjLoader wfl = new WavefrontObjLoader();
            ModelType type = ModelType.Cube;
            if (radCube.IsChecked.Value)
            {
                type = ModelType.Cube;
            }
            else if (rabSensor.IsChecked.Value)//报警器类型
            {
                type = ModelType.Sensor;
                path += "\\传感器";//报警器目录
            }
            path += "\\" + txtName.Text ;
            //判断文件是否存在
            if (File.Exists(path +".xml"))
            {
                MessageBoxResult result = MessageBox.Show("文件已存在是否覆盖", "提示", MessageBoxButton.YesNo, MessageBoxImage.Exclamation);
                if (result == MessageBoxResult.No)
                {
                    return;
                }
            }
            try
            {
                Cube cube = wfl.LoadObjFile(txtFile.Text, type);
                cube.Type = txtName.Text;
                cube.Resize = chkSize.IsChecked.Value;
                using (FileStream ms = new FileStream(path + ".xml", FileMode.Create))
                {  //保存户型图图片
                    XamlWriter.Save(cube, ms);
                }
                //缩略图
              Byte[] bt=  FileStreamReadFile(txtThumbnail.Text);
                using (FileStream fs = new FileStream(path +".jpg", FileMode.Create))
                {  //保存户型图图片
                    fs.Write(bt, 0, bt.Length);
                }
                MessageBox.Show("导入成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void ImportReSoure(string name)
        {
            if (txtName.Text.Trim().Equals(""))
            {
                MessageBox.Show("请输入名称");
                return;
            }
            if (txtFile.Text.Trim().Equals(""))
            {
                MessageBox.Show("请选择文件");
                return;
            }
            //保存路径
            string path = Directory.GetCurrentDirectory() + "\\tools";
           // WpfApplication2.WavefrontObjLoader wfl = new WpfApplication2.WavefrontObjLoader();           
            path += "\\" + txtName.Text + ".xml";
            //判断文件是否存在
            if (File.Exists(path))
            {
                MessageBoxResult result = MessageBox.Show("文件已存在是否覆盖", "提示", MessageBoxButton.YesNo, MessageBoxImage.Exclamation);
                if (result == MessageBoxResult.No)
                {
                    return;
                }
            }
            try
            {
                Model3DGroup group = (Model3DGroup)Application.Current.Resources[name];
                Cube cube = new Cube();
                cube.Content=Cube.TransformResources(group);
                using (FileStream ms = new FileStream(path, FileMode.Create))
                {  //保存户型图图片
                    XamlWriter.Save(cube, ms);
                }
                MessageBox.Show("导入成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
         /// <summary>
     /// FileStream读取文件
     /// </summary>
     /// <param name="filePath"></param>
     /// <returns></returns>
        public byte[] FileStreamReadFile(string filePath)
     {
         byte[] data ;//;
         char[] charData = new char[100];
         try
         {
             FileStream file = new FileStream(filePath, FileMode.Open);
             data = new byte[file.Length];
             //文件指针指向0位置
             file.Seek(0, SeekOrigin.Begin);
             //读入
             int count = file.Read(data, 0, (int)file.Length);
             if (count != data.Length)
             {
                 file.Close();
                 Console.WriteLine("无法从文件读取数据");
                 return data;
             }
             file.Close();//关闭读取文件流节省资源
            
             //提取字节数组
             //Decoder dec = Encoding.UTF8.GetDecoder();
             //dec.GetChars(data, 0, data.Length, charData, 0);
         }
         catch (Exception ex)
         {
             throw ex;
         }
         return data;
     }

        private void btnThumbnail_Click(object sender, RoutedEventArgs e)
        {
            
                OpenFileDialog open = new OpenFileDialog();
                open.Filter = "jpg|*.jpg|gif|*.gif|png|*.png|bmp|*.bmp|所有文件|*.*";
                if (open.ShowDialog().Value)
                {
                    try
                    {
                        txtThumbnail.Text = open.FileName;
                       // Uri uri = new Uri(open.FileName, UriKind.Absolute);
                       // BitmapImage bitmap = new BitmapImage(uri);
                        BitmapImage bitmap = new BitmapImage();
                        byte[] bytes = FileStreamReadFile(open.FileName);
                        bitmap.BeginInit();
                        bitmap.StreamSource = new MemoryStream(bytes);
                        bitmap.EndInit();
                        imgThumbnail.Source = bitmap;
                       
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
        }
    }
}
